package com.concerneyes;

import android.app.Activity;
import android.app.Application;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.os.Build;
import android.os.StrictMode;
import android.text.TextUtils;
import android.util.Log;

import com.alipay.sdk.app.EnvUtils;
import com.mob.MobSDK;

import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;

import cn.jpush.android.api.JPushInterface;
import cn.sharesdk.onekeyshare.OnekeyShare;
import http.Url;
import util.ToastHelper;
import wheelview.CityDataHelper;

/**
 * Created by Administrator on 2017/6/19 0019.
 */

public class CEApp extends Application {
    public static final String APP_ID = "1106163253";// 官方获取的APPID
    public static final String WECHAT_APP_ID = "wxc7437abe707a0530";// 官方获取的微信APPID
    public static final String LIST_PAGENUM = "20";// 官方获取的APPID
    //推送标识
    public final static String PUSH_ACTIVITY = "100";//活动
    public final static String PUSH_MESSAGE = "101";//消息通知
    public final static String PUSH_ORDER = "102";//订单
    private static CEApp appContext;
    private List<Activity> activityStack = new LinkedList<Activity>();

    public CEApp() {
    }

    public static CEApp getInstance() {
        return appContext;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        appContext = this;
        ToastHelper.getInstance().init(getApplicationContext());
        MobSDK.init(getApplicationContext(), "1fe38d51ffe61", "17d89c34c9233553da5a7a7c57f8980f");

        JPushInterface.setDebugMode(true); // 设置开启日志,发布时请关闭日志
        JPushInterface.init(this); // 初始化 JPush

        //alipay沙盒模式
//        EnvUtils.setEnv(EnvUtils.EnvEnum.SANDBOX);
        //拷贝数据库文件
        CityDataHelper dataHelper = CityDataHelper.getInstance(this);
        AssetManager assets = this.getAssets();
        try {
            InputStream in = assets.open("area_new.sqlite");
            dataHelper.copyFile(in, CityDataHelper.DATABASE_NAME, CityDataHelper.DATABASES_DIR);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
            StrictMode.setVmPolicy(builder.build());
        }
    }


    /**
     * 根据相对路径获得完整链接
     *
     * @param fileName
     * @return
     */
    public static String getResLink(String fileName) {
        if (TextUtils.isEmpty(fileName)) {
            return "";
        }
        if (fileName.startsWith("http://") || fileName.startsWith("https://")) {
            return fileName;
        }

        return Url.ImgBaseUrl + "/upload" + fileName;
    }

    /**
     * 获取版本号
     *
     * @return 当前应用的版本号
     */
    public static final String getVersion() {
        try {
            PackageManager manager = getInstance().getPackageManager();
            PackageInfo info = manager.getPackageInfo(getInstance().getPackageName(), 0);
            String version = info.versionName;
            return version;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    public void addActivity(Activity activity) {
        activityStack.add(activity);
    }

    public void removeActivity(Activity activity) {
        try {
            for (Activity act : activityStack) {
                if (act == activity) {
                    activityStack.remove(activity);
                }
            }
        } catch (Exception e) {
            Log.e(this.getClass().getSimpleName(), "removeActivity()-->" + e.toString());
        }
    }

    public void exit() {
        try {
            for (Activity activity : activityStack) {
                if (activity != null) {
                    activity.finish();
                }
            }
        } catch (Exception e) {
            Log.e(this.getClass().getSimpleName(), "exit()-->" + e.toString());
        }
    }


}
